package com.pk.platform.business.stock.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.charge.service.IChargeIndexService;
import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.business.stock.service.IStockService;
import com.pk.platform.business.stock.vo.FetchRecordVO;
import com.pk.platform.business.stock.vo.ImportRecordVO;
import com.pk.platform.business.stock.vo.StockObjVO;
import com.pk.platform.domain.charge.ChargeIndex;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.domain.stock.FetchRecord;
import com.pk.platform.domain.stock.ImportRecord;
import com.pk.platform.domain.stock.StockObj;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;

public class StockAction extends GenericAction {

	private IStockService stockService;
	
	private StockObj stockObj = new StockObj();
	
	private StockObjVO objvo = new StockObjVO();
	
	private FetchRecord fr = new FetchRecord();
	
	private FetchRecordVO frvo = new FetchRecordVO();

	private ImportRecord ir = new ImportRecord();
	
	private ImportRecordVO irvo = new ImportRecordVO();
	
	private String[] objIds;
	
	private List<Map<String, Object>> objList = new ArrayList<Map<String,Object>>();
	
	private List<ChargeIndex> indexList = new ArrayList<ChargeIndex>();
	
	private IChargeIndexService chargeIndexService;
	
	private String whickKinder;
	
	public String stockObjManage()
	{
		objvo.setKindergartenId(kindergartenId);
		lp = stockService.queryStockObj(objvo, pager);
		return SUCCESS;
	}

	public String toAddStockObj()
	{
		indexList = chargeIndexService.queryChargeIndexList();
		return SUCCESS;
	}
	
	public String addStockObj()
	{
		Kindergarten kindergarten = new Kindergarten(kindergartenId);
		User newUser = new User(this.getLoginUserId());
		Date now = new Date();
		stockObj.setRecordUser(newUser);
		stockObj.setRecordTime(now);
		stockObj.setKindergarten(kindergarten);
		stockObj.setIsDel(Constant.N_INT);
		if(!StringConverter.isNotNull(stockObj.getChargeIndex().getId()))
		{
			stockObj.setChargeIndex(null);
		}
		stockService.addStockObj(stockObj, whickKinder);
		return SUCCESS;
	}
	
	public String toUpdateStockObj()
	{
		stockObj = stockService.queryStockObjById(stockObj.getId());
		indexList = chargeIndexService.queryChargeIndexList();
		return SUCCESS;
	}
	
	public String updateStockObj()
	{
		if(!StringConverter.isNotNull(stockObj.getChargeIndex().getId()))
		{
			stockObj.setChargeIndex(null);
		}
		stockService.updateStockObj(stockObj);
		return SUCCESS;
	}
	
	public String delStockObj()
	{
		if(objIds != null)
		{
			stockService.delStockObj(objIds);
		}
		return SUCCESS;
	}
	
	
	
	//
	public String fetchRecordManage()
	{
		objList = stockService.queryStockObjList(kindergartenId);
		frvo.setKindergartenId(kindergartenId);
		lp = stockService.queryFetchRecord(frvo, pager);
		return SUCCESS;
	}

	public String toAddFetchRecord()
	{
		objList = stockService.queryStockObjList(kindergartenId);
		return SUCCESS;
	}
	
	public String addFetchRecord()
	{
		User newUser = new User(this.getLoginUserId());
		Kindergarten kindergarten = new Kindergarten(kindergartenId);
		fr.setKindergarten(kindergarten);
		fr.setRecordUser(newUser);
		fr.setRecordTime(new Date());
		message = stockService.addFetchRecord(fr);
		return SUCCESS;
	}
	
	public String toUpdateFetchRecord()
	{
		objList = stockService.queryStockObjList(kindergartenId);
		fr = stockService.queryFetchRecordById(fr.getId());
		return SUCCESS;
	}
	
	public String delFetchRecord()
	{
		stockService.delFetchRecord(fr.getId());
		return SUCCESS;
	}
	
	
	//
	public String importRecordManage()
	{
		objList = stockService.queryStockObjList(kindergartenId);
		irvo.setKindergartenId(kindergartenId);
		lp = stockService.queryImportRecord(irvo, pager);
		return SUCCESS;
	}

	public String toAddImportRecord()
	{
		objList = stockService.queryStockObjList(kindergartenId);
		return SUCCESS;
	}
	
	public String addImportRecord()
	{
		User newUser = new User(this.getLoginUserId());
		Kindergarten kindergarten = new Kindergarten(kindergartenId);
		ir.setKindergarten(kindergarten);
		ir.setRecordUser(newUser);
		ir.setRecordTime(new Date());
		stockService.addImportRecord(ir);
		return SUCCESS;
	}
	
	public String toUpdateImportRecord()
	{
		objList = stockService.queryStockObjList(kindergartenId);
		ir = stockService.queryImportRecordById(ir.getId());
		return SUCCESS;
	}
	
	public String delImportRecord()
	{
		message = stockService.delImportRecord(ir.getId());
		return SUCCESS;
	}
	
	
	
	
	

	public IStockService getStockService() {
		return stockService;
	}

	public void setStockService(IStockService stockService) {
		this.stockService = stockService;
	}

	public StockObj getStockObj() {
		return stockObj;
	}

	public void setStockObj(StockObj stockObj) {
		this.stockObj = stockObj;
	}

	public StockObjVO getObjvo() {
		return objvo;
	}

	public void setObjvo(StockObjVO objvo) {
		this.objvo = objvo;
	}

	public FetchRecord getFr() {
		return fr;
	}

	public void setFr(FetchRecord fr) {
		this.fr = fr;
	}

	public FetchRecordVO getFrvo() {
		return frvo;
	}

	public void setFrvo(FetchRecordVO frvo) {
		this.frvo = frvo;
	}

	public ImportRecord getIr() {
		return ir;
	}

	public void setIr(ImportRecord ir) {
		this.ir = ir;
	}

	public ImportRecordVO getIrvo() {
		return irvo;
	}

	public void setIrvo(ImportRecordVO irvo) {
		this.irvo = irvo;
	}

	public String[] getObjIds() {
		return objIds;
	}

	public void setObjIds(String[] objIds) {
		this.objIds = objIds;
	}

	public List<Map<String, Object>> getObjList() {
		return objList;
	}

	public void setObjList(List<Map<String, Object>> objList) {
		this.objList = objList;
	}

	public List<ChargeIndex> getIndexList() {
		return indexList;
	}

	public void setIndexList(List<ChargeIndex> indexList) {
		this.indexList = indexList;
	}

	public IChargeIndexService getChargeIndexService() {
		return chargeIndexService;
	}

	public void setChargeIndexService(IChargeIndexService chargeIndexService) {
		this.chargeIndexService = chargeIndexService;
	}

	public String getWhickKinder() {
		return whickKinder;
	}

	public void setWhickKinder(String whickKinder) {
		this.whickKinder = whickKinder;
	}
	
}
